package com.common.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.common.R;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * Created by wqf on 16/8/25.
 */
public abstract class BaseFragment extends Fragment implements IView, Toolbar.OnMenuItemClickListener {
    
    protected Activity mActivity;
    
    private ProgressDialog mDialog;
    private View mRootView;
    private Unbinder mUnbinder;
    private Toast mToast;
    private Toolbar mToolbar;
    private TextView mToolbarTitle;
    
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        mRootView = inflater.inflate(getLayoutId(), container, false);
        bindView();
        return mRootView;
    }
    
    protected abstract int getLayoutId();
    
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initEnv();
        setUpView();
        setUpData();
    }
    
    public void initEnv() {
        
    }
    
    public void setUpToolbar(int titleId, int menuId, int mode) {
        setUpToolbar(titleId > 0 ? getString(titleId) : "", menuId, mode);
    }
    
    public void setUpToolbar(String titleStr, int menuId, int mode) {
        
        mToolbar = (Toolbar) getView().findViewById(R.id.toolbar);
        if (mToolbar == null) {
            return;
        }
        mToolbar.setTitle("");
        mToolbarTitle = (TextView) getView().findViewById(R.id.toolbar_title);
        
        if (mode == MODE_BACK) {
            mToolbar.setNavigationIcon(R.drawable.ic_return);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onNavigationBtnClicked();
                }
            });
        }
        
        if (!TextUtils.isEmpty(titleStr)) {
            setUpTitle(titleStr);
        }
        if (menuId > 0) {
            setUpMenu(menuId);
        }
    }
    
    private void onNavigationBtnClicked() {
        
    }
    
    /**
     * @param menuId
     */
    protected void setUpMenu(int menuId) {
        if (mToolbar != null) {
            mToolbar.getMenu().clear();
            if (menuId > 0) {
                mToolbar.inflateMenu(menuId);
                mToolbar.setOnMenuItemClickListener(this);
            }
        }
    }
    
    protected void setUpTitle(String titleStr) {
        if (mToolbarTitle != null) {
            mToolbarTitle.setText(titleStr);
        }
    }
    
    @Override
    public boolean onMenuItemClick(MenuItem item) {
        return false;
    }
    
    @Override
    public void bindView() {
        mUnbinder = ButterKnife.bind(this, mRootView);
    }
    
    @Override
    public boolean isActivityFinished() {
        Activity activity = getActivity();
        //Activity被回收导致fragment的getActivity为null的解决办法
        if (Build.VERSION.SDK_INT >= 17) {
            return activity == null || activity.isFinishing() || activity.isDestroyed();
        } else {
            return activity == null || activity.isFinishing();
        }
    }
    
    @Override
    public void showLoading() {
        if (mDialog == null) {
            mDialog = new ProgressDialog(getContext());
        }
        mDialog.show();
    }
    
    @Override
    public void dismissLoading() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }
    
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnbinder.unbind();
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
        if (mActivity != null) {
            mActivity = null;
        }
    }
    
    @Override
    public void showToast(String msg) {
        if (mToast != null) {
            mToast.cancel();
        }
        mToast = Toast.makeText(getContext(), msg, Toast.LENGTH_LONG);
        mToast.show();
    }
}
